//
//  BasicNavigationController.m
//  HuaShun
//
//  Created by Fuhao on 16/8/10.
//  Copyright © 2016年 Phenix. All rights reserved.
//

#import "BasicNavigationController.h"
#import "UIBarButtonItem+Tool.h"
@interface BasicNavigationController ()<UINavigationControllerDelegate>

@property(nonatomic ,strong) id popDelegate;

@end

@implementation BasicNavigationController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.view.backgroundColor = [UIColor whiteColor];
    //滑动返回
    _popDelegate = self.interactivePopGestureRecognizer.delegate;
    
    self.delegate = self;
    //不透明
//    self.navigationBar.translucent = NO;
    
    //设置不需要额外的滚动区域
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self NavigationUIinit];

    self.navigationBar.shadowImage = [UIImage new];
//    //1.设置阴影颜色
//    self.navigationBar.layer.shadowColor = HEXCOLOR(0x262a36).CGColor;
//    //2.设置阴影偏移范围
//    self.navigationBar.layer.shadowOffset = CGSizeMake(0, 3);
//    //3.设置阴影颜色的透明度
//    self.navigationBar.layer.shadowOpacity = 0.2;
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:KMainColor] forBarPosition:UIBarPositionAny
                                                     barMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    self.navigationBar.barTintColor = KWhiteColor;
//    //4.设置阴影半径
//    self.navigationController.navigationBar.layer.shadowRadius = 16;
//    //5.设置阴影路径
//    self.navigationController.navigationBar.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.navigationController.navigationBar.bounds].CGPath;
    
    
}
-(void)NavigationUIinit
{
    NSMutableDictionary *titleAttrs = [NSMutableDictionary dictionary];
    titleAttrs[NSForegroundColorAttributeName] =  KWhiteColor;
    titleAttrs[NSFontAttributeName] =   SBFONT(18);
//    CLRudeCircleTextFont(18);
//
    
    
    self.navigationBar.titleTextAttributes = titleAttrs;
    //设置整个项目所有的item多主题样式
    UIBarButtonItem *item = [UIBarButtonItem appearance];
    //设置普通状态
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSFontAttributeName] = BFONT(18);
    textAttrs[NSForegroundColorAttributeName] = TextTiTleColor;
    [item setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
    //设置高亮状态
    NSMutableDictionary *highTextAttrs = [NSMutableDictionary dictionary];
    highTextAttrs[NSFontAttributeName] = BFONT(18);
    highTextAttrs[NSForegroundColorAttributeName] = KMainColor;
    [item setTitleTextAttributes:highTextAttrs forState:UIControlStateHighlighted];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


//写这个方法目的：能够拦截所有push进来的控制器viewController
-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    if (self.viewControllers.count > 0) {//这时push的是子控制器（不是根控制器）
        viewController.hidesBottomBarWhenPushed = YES;//隐藏tabbar
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.backgroundColor = [UIColor clearColor]; //加上背景颜色，方便观察Button的大小
        [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        //设置图片
        UIImage *imageForButton = [UIImage imageNamed:@"箭头_白色"];
        [button setImage:imageForButton forState:UIControlStateNormal];
        //设置文字
        NSString *buttonTitleStr = [UserDefaultStandard objectForKey:@"backTitle"];
        //设置后清空
        [UserDefaultStandard setObject:@"" forKey:@"backTitle"];
        [button setTitle:buttonTitleStr forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:20];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        button.contentHorizontalAlignment =UIControlContentHorizontalAlignmentLeft;
        button.frame = CGRectMake(0, 0 ,30, 100);
        [button setTitleEdgeInsets:UIEdgeInsetsMake(0,4 *kScreenWidth/375.0,0,0)];
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    
//        [[UIBarButtonItem alloc] initWithImage:[UIImage imageWithOriginalName:@"fanhui"] style:UIBarButtonItemStyleDone target:self action:@selector(back)];
    }
    
    [super pushViewController:viewController animated:animated];
}
//滑动返回
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
    if (viewController == [self.viewControllers firstObject]) {
        self.interactivePopGestureRecognizer.delegate = _popDelegate;
    }else{
        self.interactivePopGestureRecognizer.delegate = nil;
    }
}
- (void)back
{
    // 因为self本来就是一个导航控制器，self.navigationController这里是nil的
    [self popViewControllerAnimated:YES];
}
- (UIViewController *)childViewControllerForStatusBarStyle {
    return self.visibleViewController;
}

@end
